<template>
 <div class="login-wrap">
  <el-form  status-icon :model="form" :rules="rules"  ref="ruleForm" label-width="100px" class="login-from">
    <h2>用户登录</h2>
    <el-form-item label="用户名" prop="username">
      <el-input type="text" v-model="form.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="form.password" ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary"   @click="submit('ruleForm')">登录</el-button>
      <router-link to="register"><el-button type="primary">去注册</el-button></router-link>
    </el-form-item>
  </el-form>
 </div>
</template>

<script>
// import Axios from 'axios'

export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit (ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          var data = this.$qs.stringify(this.form)
          this.$http.post('http://120.27.129.163/iblog/index.php/login', data)
            .then(backdata => {
              var data = backdata.data
              if (data.code === 200) {
                this.$message({
                  showClose: true,
                  message: data.msg,
                  type: 'success'
                })
                window.localStorage.setItem('userinfo', JSON.stringify(data.data))
                this.$router.push({name: 'Index'})
              } if (data.code === 500) {
                this.$message({
                  showClose: true,
                  message: data.msg,
                  type: 'error'
                })
              } if (data.code === 501) {
                this.$message.error(data.msg)
              }
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style>
  .login-wrap {
  background-color: #324152;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  }
  .login-wrap .login-from {
  background-color: #fff;
  width: 400px;
  padding: 30px;
  border-radius: 5px;
  }
  .login-wrap .login-from .login-btn {
  width: 100%;
  }
</style>
